<template>
  <el-form
    v-if="config.length > 0"
    ref="advenceFilterForm"
    :inline="true"
    :model="filterModel"
    size="small"
    class="filter-container"
    label-position="left"
    label-width="25%"
    @submit.native.prevent
  >
    <div class="flex-form-item">
      <filter-item
        v-for="(item, index) in config"
        :key="index"
        v-model="filterModel[item.prop]"
        :item="item"
      >
        <!--组合类型-->
        <div
          v-if="item.children && item.children.length"
          class="combine"
        >
          <filter-item
            v-for="(gitem, gindex) in item.children"
            :key="gindex"
            v-model="filterModel[gitem.prop]"
            :item="gitem"
          />
        </div>
      </filter-item>
    </div>
    <div class="flex-form-item">
      <el-form-item label=" ">
        <el-button
          v-waves
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >查询</el-button>
        <el-button @click="resetForm('advenceFilterForm')">重置</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
import FilterItem from './filterItem'
export default {
  name: 'FilterAdvenceForm',
  components: {
    FilterItem
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    config: {
      type: Array,
      default() {
        return []
      }
    },
    value: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  data() {
    return {
      filterModel2: {}
    }
  },
  computed: {
    filterModel: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  watch: {
    /* 实现表单数据的绑定，实时接收父组件的数据变化*/
    // filterData(v) {
    //   console.log('filterData', v)
    //   this.filterModel = this.filterData
    // }
  },
  mounted() {
    // 将组件上的属性赋值给当前组件内变量，因为props只能单向绑定,还需要监听属性值变化进行父子组件间交互
    // this.filterModel = this.filterData
  },
  methods: {
    // 提交表单数据
    handleFilter() {
      console.log('handleFilter', this.filterModel)
      this.$emit('filter', this.filterModel)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding: 15px;
  margin-bottom: 10px;
  background-color: #f5f7fd;
  border-radius: 5px;
  .el-input {
    width: 100% !important;
  }

  .el-select {
    width: 100% !important;
  }

  ::v-deep .el-form-item {
    display: flex;
    margin: 0;
    &__label {
      font-size: 12px;
    }
    &__content {
      flex: 1;
    }
    & + .el-form-item {
      margin: 0;
    }
    .el-range-editor--small.el-input__inner {
      width: auto;
    }
  }
}

.flex-form-item {
  display: grid;
  gap: 15px 2%;
  grid-template-columns: repeat(3, 32%);
  & + .flex-form-item {
    margin-top: 15px;
  }
}
</style>
